package com.jungle.form.control;

import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Control;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;

import java.util.List;

public class FormCheckBoxGroup extends Control {

    // List<String>/List<Integer>/List<Option<String,Integer>>
    private final ToggleGroup group = new ToggleGroup();

    // 横向布局、纵向布局、流式布局
    private List<CheckBox> checkBoxes;

    public FormCheckBoxGroup() {
    }

    public void initFlowPane(List<CheckBox> checkBoxes) {
        FlowPane flowPane = new FlowPane();
        flowPane.setAlignment(Pos.CENTER); // 设置对齐方式为居中
        flowPane.setHgap(10); // 设置水平间隔
        flowPane.setVgap(10); // 设置垂直间隔
        for (CheckBox checkBox : checkBoxes) {
            flowPane.getChildren().add(checkBox);
        }
        // 添加多个按钮到 FlowPane
        // 将 FlowPane 设置为自定义控件的皮肤
        this.setSkin(new FlowControlSkin(flowPane));
    }

    public void initGridPane(List<CheckBox> checkBoxes) {
        GridPane gridPane = new GridPane();
        gridPane.setAlignment(Pos.CENTER); // 设置对齐方式为居中
        gridPane.setHgap(10); // 设置水平间隔
        gridPane.setVgap(10); // 设置垂直间隔
        gridPane.setPadding(new Insets(10));
        for (int i = 0; i < checkBoxes.size(); i++) {
            checkBoxes.get(i).setText("选项" + (i + 1));
            gridPane.add(checkBoxes.get(i), i % 2, i / 2);
        }
        // 添加多个按钮到 FlowPane
        // 将 FlowPane 设置为自定义控件的皮肤
        this.setSkin(new GridControlSkin(gridPane));
    }
}

class FlowControlSkin extends javafx.scene.control.SkinBase<FormCheckBoxGroup> {
    public FlowControlSkin(FlowPane flowPane) {
        super(new FormCheckBoxGroup());
        this.getChildren().add(flowPane);
    }
}

class GridControlSkin extends javafx.scene.control.SkinBase<FormCheckBoxGroup> {
    public GridControlSkin(GridPane gridPane) {
        super(new FormCheckBoxGroup());
        this.getChildren().add(gridPane);
    }
}